use "/Users/jimgolby/Desktop/ZB - Confidence Book/Technical Appendix/Data Files/Wave 1 Weighted.dta"

***Defining Variables***
replace q8 =. if q8 > 4

generate q8a = .
replace q8a = 0
replace q8a = 1 if q8 < 3

label define CONFIDENCE 0 "Not Confident" 1 "Confidence"
label values q8a CONFIDENCE

generate pid3 =.
replace pid3 = 1 if partyid7 < 4
replace pid3 = 2 if partyid7 == 4
replace pid3 = 3 if partyid7 > 4

label define PARTY 1 "Democrats" 2 "Independents" 3 "Republicans"
label values pid3 PARTY

generate dem = 0 if pid3 < 4
replace dem = 1 if pid3 == 1
generate rep = 0 if pid3 < 4
replace rep = 1 if pid3 == 3
generate ind = 0 if pid3 < 4
replace ind = 1 if pid3 == 2

generate ideo3 =.
replace ideo3 = 1 if ideo < 4
replace ideo3 = 2 if ideo == 4
replace ideo3 = 3 if ideo > 4 & ideo < 8

generate ideo3a = ideo3
replace ideo3a = 4 if ideo == 8

gen lib = 0
replace lib = 1 if ideo3 == 1
gen mod = 0
replace mod = 1 if ideo3 == 2
gen con = 0
replace con = 1 if ideo3 == 3

generate woman = 0
replace woman = 1 if gender == 2
generate male = 0
replace male = 1 if gender == 1

gen vet = veteran if veteran < 98
replace vet = 0 if veteran == 2
replace vet = 0 if veteran2 == 1
replace vet =. if veteran2 == 98

gen activeduty = 0 if veteran < 98
replace activeduty =. if veteran2 == 98
replace activeduty = 1 if veteran2 == 1

generate milvet= 0
replace milvet = 1 if vet == 1
replace milvet = 2 if activeduty == 1

generate post911vet = 0
replace post911vet = 1 if milvet > 0 & vet_date1 < 2001
replace post911vet = 2 if milvet > 0 & vet_date1 > 2000

generate social = 0 if q45 < 98
replace social = 1 if q45 == 1

generate family = 0 if q46 < 98
replace family = 1 if q46 == 1
replace family = . if q46b_dk == 1

generate religion = 0 if relig < 98
replace religion = 1 if relig == 1
replace religion = 2 if relig == 2
replace religion = 3 if relig > 8 & relig < 12
replace religion = 1 if relig == 12

label define RELIGION2 0 "Other" 1 "Christian" 2 "Catholic" 3 "None"
label values religion RELIGION2

generate christian = 0
replace christian = 1 if religion == 1
generate catholic = 0
replace catholic = 1 if religion == 2
generate norelig = 0
replace norelig = 1 if religion == 3

generate white = 0
replace white = 1 if racethnicity == 1
generate black = 0
replace black = 1 if racethnicity == 2
replace black = 1 if racethnicity == 3
generate hispanic = 0
replace hispanic = 1 if racethnicity == 4
generate asian = 0
replace asian = 1 if racethnicity == 6

gen race2 = racethnicity
replace race2 =. if racethnicity == 3
replace race2 =. if racethnicity == 5

label define RACE 1 "White" 2 "Black" 4 "Hispanic" 6 "Asian"
label values race2 racethnicity RACE

generate generation =.
replace generation = 1 if age > 73
replace generation = 2 if age > 54 & age < 74
replace generation = 3 if age > 38 & age < 55
replace generation = 4 if age > 22 & age < 39
replace generation = 5 if age < 23

generate silent = 0
replace silent = 1 if generation == 1
generate boomer = 0
replace boomer = 1 if generation == 2
generate genx = 0
replace genx = 1 if generation == 3
generate milen = 0
replace milen = 1 if generation == 4
generate genz = 0
replace genz = 1 if generation == 5

generate northeast = 0
replace northeast = 1 if region4 == 1
generate midwest = 0
replace midwest = 1 if region4 == 2
generate south = 0
replace south = 1 if region4 == 3 
generate west = 0
replace west = 1 if region4 == 4 

generate city = 0
replace city = 1 if urban3 == 1
generate suburban = 0
replace suburban = 1 if urban3 == 2
generate rural = 0
replace rural = 1 if urban3 == 3

generate unemployed = 0 if employ < 3
replace unemployed = 1 if employ > 2
replace unemployed = 0 if employ == 5

generate income5 =.
replace income5 = 1 if income < 5
replace income5 = 2 if income > 4 & income < 9
replace income5 = 3 if income > 8 & income < 12
replace income5 = 4 if income > 11 & income < 15
replace income5 = 5 if income > 14

gen married =.
replace married = 1 if marital == 1
replace married = 0 if marital > 1

replace q14=. if q14 > 76
replace age=. if age == 286

generate q1aCOR = 0 if q1a < 77
replace q1aCOR = 1 if q1a == 3
generate q1bCOR = 0 if q1b < 77
replace q1bCOR = 1 if q1b == 4
generate q2aCOR = 0 if q2a < 77
replace q2aCOR = 1 if q2a == 3
generate q2bCOR = 0  if rnd_02 == 1
replace q2bCOR = 1 if q2b_1 == 1 & q2b_2 == 1 & q2b_4 == 1 & q2b_5 == 1 
generate q3COR = 0 if q3 < 98
replace q3COR = 1 if q3 == 3
generate q4COR = 0 if q4 < 98
replace q4COR = 1 if q4 == 1
generate q5COR = 0  if q5 < 98
replace q5COR = 1 if q5 == 2
generate q6COR = 0  if q6 < 98
replace q6COR = 1 if q6 == 3

generate q1aCOR2 = q1aCOR
replace q1aCOR2 = 0 if q1aCOR == .
replace q1aCOR2 = . if q1aCOR == 98

generate q1bCOR2 = q1bCOR
replace q1bCOR2 = 0 if q1bCOR == .
replace q1bCOR2 = . if q1bCOR == 98

generate q2aCOR2 = q2aCOR
replace q2aCOR2 = 0 if q2aCOR == .
replace q2aCOR2 = . if q2aCOR == 98

generate q2bCOR2 = q2bCOR
replace q2bCOR2 = 0 if q2bCOR == .
replace q2bCOR2 = . if q2bCOR == 98


generate q2bCORa = .
replace q2bCORa = q2b_1
replace q2bCORa = 1 if q2b_1 == 0 & q2b_2 == 1
replace q2bCORa = 1 if q2b_1 == 0 & q2b_2 == 0 & q2b_5 == 1

generate q2bCORb = .
replace q2bCORb = 0 if q2b_1 < 98
replace q2bCORb = 1 if q2b_1 == 1 & q2b_2 == 1
replace q2bCORb = 1 if q2b_1 == 1 & q2b_5 == 1
replace q2bCORb = 1 if q2b_2 == 1 & q2b_5 == 1

generate knowledge = (q1aCOR2 + q1bCOR2 + q2aCOR2 + q2bCOR2 + q3COR + q4COR + q5COR + q6COR)
gen k2 = knowledge/6

*****Reversed for Table*****
replace q18 =. if q18 == 98

gen q18a = 0 if q18 < 77
replace q18a = 1 if q18 > 2 & q18 < 77

gen d2 = 0
gen d3 = 0
gen d4 = 0
gen d5 = 0
gen d6 = 0
gen d7 = 0
gen d8 = 0

replace d2 = 1 if dov_assignment_a == 2
replace d3 = 1 if dov_assignment_a == 3
replace d4 = 1 if dov_assignment_a == 4
replace d5 = 1 if dov_assignment_a == 5
replace d6 = 1 if dov_assignment_a == 6
replace d7 = 1 if dov_assignment_a == 7
replace d8 = 1 if dov_assignment_a == 8

label define LOOKS 1 "Mil Looks Like Me" 0 "Doesn't Look Like Me"
label values q18a LOOKS

gen educ5 = educ4
replace educ5 = 5 if educ > 12 & educ < 15

xi I.dem*educ5 I.rep*educ5 I.dem*k2 I.rep*k2, prefix(_I)

gen q16a3 = q16a if q16a < 4

gen q16aR = 0 if q16a3 < 4
replace q16aR = 1 if q16a3 == 2

gen q16aREP = q16aR
replace q16aREP = 0 if q16b == 1
replace q16aREP = 0 if q16b == 3
replace q16aREP = 1 if q16b == 2

replace q44 =. if q44 == 98
replace q45 =. if q45 == 98
replace q46 =. if q46 == 98

gen Q46 = .
replace Q46 = 1 if q46a_1 == 1
replace Q46 = 2 if q46a_2 == 1
replace Q46 = 3 if q46a_3 == 1
replace Q46 = 4 if q46a_4 == 1
replace Q46 = 5 if q46a_5 == 1

gen Q46FAM = 0 if q46 < 98
replace Q46FAM = 1 if q46b_1 == 1
replace Q46FAM = 2 if q46b_2 == 1
replace Q46FAM = 3 if q46b_3 == 1
replace Q46FAM = . if q46b_dk == 77

gen Q46FAM1 = 0 if Q46FAM < 4
replace Q46FAM1 = 1 if Q46 == 1

gen Q46FAMpre = 0 if Q46FAM < 4
replace Q46FAMpre = 1 if Q46FAM == 1
gen Q46FAMpost = 0 if Q46FAM < 4
replace Q46FAMpost = 1 if Q46FAM == 2
gen Q46FAMboth = 0 if Q46FAM < 4
replace Q46FAMboth = 0 if Q46FAM == 2
replace Q46FAMboth = 1 if Q46FAM == 3

gen Q46FAM2pre = 0 if Q46FAM < 4
replace Q46FAM2pre = 1 if Q46FAM == 1
gen Q46FAM2post = 0 if Q46FAM < 4
replace Q46FAM2post = 1 if Q46FAMpost == 1
replace Q46FAM2post = 1 if Q46FAMboth == 1


********************
*****Table 3.1*****
tab q18 race2 if q18 < 98 & dov_assignment_a == 1 [aweight=weight], column

*****Figure 3.1*****
graph bar (mean) q8a [pweight = weight], over(q18a, label(labcolor("black") angle(vertical) labsize(vsmall))) over(race2, label(labcolor("black") labsize(small) labgap(0)) axis(outergap(0))) bar(1, fcolor(gs12) lcolor(black)) blabel(bar, color(black) format(%3.2f)) ytitle(% Expressing Confidence in the Military) ytitle(, size(small)) yscale(range(0 1)) ylabel(0(0.2)1, nogrid) ymtick(, nogrid glcolor()) graphregion(fcolor(white) lcolor(white) lalign(outside) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(black) lalign(outside) ifcolor(white) ilcolor(white))

logit q8a q18a dem rep ideo3 male activeduty vet family social catholic christian norelig white black hispanic asian educ5 boomer genx milen genz midwest south west city rural unemployed income5 married d2 d3 d4 d5 d6 d7 d8 [pweight = weight]

*****Table 3.2*****
mean q1aCOR [pweight=weight], cformat(%3.2f)
mean q1bCOR [pweight=weight], cformat(%3.2f)
mean q2aCOR [pweight=weight], cformat(%3.2f)
mean q2bCOR [pweight=weight], cformat(%3.2f)
mean q3COR [pweight=weight], cformat(%3.2f)
mean q4COR [pweight=weight], cformat(%3.2f)
mean q5COR [pweight=weight], cformat(%3.2f)
mean q6COR [pweight=weight], cformat(%3.2f)
mean k2 [pweight=weight], cformat(%3.2f)


tab q1aCOR if duration > 9 [aweight=weight]
tab q1bCOR if duration > 9 [aweight=weight]
tab q2aCOR if duration > 9 [aweight=weight]
tab q2bCOR if duration > 9 [aweight=weight]
tab q3COR if duration > 9 [aweight=weight]
tab q4COR if duration > 9 [aweight=weight]
tab q5COR if duration > 9 [aweight=weight]
tab q6COR if duration > 9 [aweight=weight]
mean k2 if duration > 9 [aweight=weight]

*****Figure 3.2*****
graph bar if q8a == 0 [pweight = weight], over(knowledge) bar(1, fcolor(gs12) lcolor(black)) blabel(bar, color(black) format(%3.0f)) ytitle(% Answering Correctly) ytitle(, size(medsmall)) yscale(range(0 50)) ylabel(0(10)50, nogrid) ymtick(, nogrid glcolor()) graphregion(fcolor(white) lcolor(white) lalign(outside) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(black) lalign(outside) ifcolor(white) ilcolor(white)) subtitle(Number of Correct Questions -- Low Confidence, size(medsmall) color(black) position(6)) name(conf1, replace)

graph bar if q8a == 1 [pweight = weight], over(knowledge) bar(1, fcolor(gs12) lcolor(black)) blabel(bar, color(black) format(%3.0f)) ytitle(% Answering Correctly) ytitle(, size(medsmall)) yscale(range(0 50)) ylabel(0(10)50, nogrid) ymtick(, nogrid glcolor()) graphregion(fcolor(white) lcolor(white) lalign(outside) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(black) lalign(outside) ifcolor(white) ilcolor(white)) subtitle(Number of Correct Questions -- High Confidence, size(medsmall) color(black) position(6)) name(conf2, replace)

graph combine conf1 conf2, rows(2) name(confcombine, replace)

logit q8a k2 q18a dem rep ideo3 male activeduty vet family social catholic christian norelig white black hispanic asian educ5 boomer genx milen genz midwest south west city rural unemployed income5 married d2 d3 d4 d5 d6 d7 d8 [pweight = weight]



****education*****

logit q8a k2 dem rep ideo3 male activeduty vet family social catholic christian norelig white black hispanic asian educ5 _IrepXeduc5_1 _IdemXeduc5_1 boomer genx milen genz midwest south west city rural unemployed income5 married d2 d3 d4 d5 d6 d7 d8 [pweight = weight]

margins, at(vet=0 social=0 family=0 dem=1 rep=0 educ5=1 _IrepXeduc5_1=0 _IdemXeduc5_1=1 male=1 black=0 hispanic=0 asian=0 south=0 married=1 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=1 rep=0 educ5=2 _IrepXeduc5_1=0 _IdemXeduc5_1=2 male=1 black=0 hispanic=0 asian=0 south=0 married=1 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=1 rep=0 educ5=3 _IrepXeduc5_1=0 _IdemXeduc5_1=3 male=1 black=0 hispanic=0 asian=0 south=0 married=1 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=1 rep=0 educ5=4 _IrepXeduc5_1=0 _IdemXeduc5_1=4 male=1 black=0 hispanic=0 asian=0 south=0 married=0 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=1 rep=0 educ5=5 _IrepXeduc5_1=0 _IdemXeduc5_1=5 male=1 black=0 hispanic=0 asian=0 south=0 married=0 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) post
est store Dem1

logit q8a k2 dem rep ideo3 male activeduty vet family social catholic christian norelig white black hispanic asian educ5 _IrepXeduc5_1 _IdemXeduc5_1 boomer genx milen genz midwest south west city rural unemployed income5 married d2 d3 d4 d5 d6 d7 d8 [pweight = weight]

margins, at(vet=0 social=0 family=0 dem=0 rep=1 educ5=1 _IrepXeduc5_1=1 _IdemXeduc5_1=0 male=1 black=0 hispanic=0 asian=0 south=0 married=1 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=0 rep=1 educ5=2 _IrepXeduc5_1=2 _IdemXeduc5_1=0 male=1 black=0 hispanic=0 asian=0 south=0 married=1 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=0 rep=1 educ5=3 _IrepXeduc5_1=3 _IdemXeduc5_1=0 male=1 black=0 hispanic=0 asian=0 south=0 married=1 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=0 rep=1 educ5=4 _IrepXeduc5_1=4 _IdemXeduc5_1=0 male=1 black=0 hispanic=0 asian=0 south=0 married=0 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=0 rep=1 educ5=5 _IrepXeduc5_1=5 _IdemXeduc5_1=0 male=1 black=0 hispanic=0 asian=0 south=0 married=0 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) post
est store Rep1

coefplot Dem1 Rep1, vertical lwidth(*0.5) connect(l) ytitle(% Answering Correctly) ytitle(, size(medsmall)) yscale(range(0 1)) ylabel(0(.2)1, nogrid) ymtick(, nogrid glcolor()) graphregion(fcolor(white) lcolor(white) lalign(outside) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(black) lalign(outside) ifcolor(white) ilcolor(white)) subtitle(Education Level, size(medsmall) color(black) position(6)) name(edconf, replace)

***********Ed & Party*********
logit q16aREP knowledge vet social family dem rep educ5 _IrepXeduc5_1 _IdemXeduc5_1 ideo3 male black hispanic asian income age south married rural city d2 d3 d4 d5 d6 d7 d8 [pweight=weight]
margins, at(vet=0 social=0 family=0 dem=1 rep=0 educ5=1 _IrepXeduc5_1=0 _IdemXeduc5_1=1 male=1 black=0 hispanic=0 asian=0 south=0 married=1 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=1 rep=0 educ5=2 _IrepXeduc5_1=0 _IdemXeduc5_1=2 male=1 black=0 hispanic=0 asian=0 south=0 married=1 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=1 rep=0 educ5=3 _IrepXeduc5_1=0 _IdemXeduc5_1=3 male=1 black=0 hispanic=0 asian=0 south=0 married=1 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=1 rep=0 educ5=4 _IrepXeduc5_1=0 _IdemXeduc5_1=4 male=1 black=0 hispanic=0 asian=0 south=0 married=0 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) post
est store Dem2

logit q16aREP knowledge vet social family dem rep educ5 _IrepXeduc5_1 _IdemXeduc5_1 ideo3 male black hispanic asian income age south married rural city d2 d3 d4 d5 d6 d7 d8 [pweight=weight]
margins, at(vet=0 social=0 family=0 dem=0 rep=1 educ5=1 _IrepXeduc5_1=1 _IdemXeduc5_1=0 male=1 black=0 hispanic=0 asian=0 south=0 married=1 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=0 rep=1 educ5=2 _IrepXeduc5_1=2 _IdemXeduc5_1=0 male=1 black=0 hispanic=0 asian=0 south=0 married=1 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=0 rep=1 educ5=3 _IrepXeduc5_1=3 _IdemXeduc5_1=0 male=1 black=0 hispanic=0 asian=0 south=0 married=1 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) at(vet=0 social=0 family=0 dem=0 rep=1 educ5=4 _IrepXeduc5_1=4 _IdemXeduc5_1=0 male=1 black=0 hispanic=0 asian=0 south=0 married=0 rural=0 city=0 d2=0 d3=0 d4=0 d5=0 d6=0 d7=0 d8=0) post
est store Rep2

coefplot Dem2 Rep2, vertical lwidth(*0.5) connect(l) ytitle(Predicted % Who Think the Military is Republican) ytitle(, size(medsmall)) yscale(range(0 1)) ylabel(0(.2)1, nogrid) ymtick(, nogrid glcolor()) graphregion(fcolor(white) lcolor(white) lalign(outside) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(black) lalign(outside) ifcolor(white) ilcolor(white)) subtitle(Education Level, size(medsmall) color(black) position(6)) name(edparty, replace)
